package tests;

import static org.junit.Assert.*;

import java.util.List;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Test;

import classes.Market;
import dao.MarketDAO;
import dao.DAOFactory;
import webservices.MarketResource;

import com.sun.jersey.api.client.*;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class MarketDAOTest {
	private static Market m;
	private static WebResource service;
	
	
	@BeforeClass
	public static void fixtureSetUp() throws Exception{
		DAOFactory.connect();
	}
	
	@AfterClass
	public static void fixtureTearDown() throws Exception{
		DAOFactory.disconnect();
	}
	
	@Before
	public void setUp() throws Exception{
		ClientConfig config = new DefaultClientConfig();
	    Client client = Client.create(config);
	    service = client.resource(UriBuilder.fromUri("http://localhost:8080/eKupovina").build());
	}
	@Test 
	public void dajSveMarkete() throws Exception{
		try{
			List<Market> marketi= service.path("webservices").path("marketi").path("svi").accept(MediaType.APPLICATION_XML).get(new  GenericType<List<Market>>() {});
			assertNotNull(marketi.size());
		}catch(Exception ex){
			System.out.println(ex.getMessage());
		}
	}
	/**
	@Test
	public void testInsertMarket() throws Exception{
		m = new Market();
		m.setNaziv("DP");
		m.setUsername("Dp");
		m.setPassword("Dp");
		String user = "edin";
		String pass = "edin";
		try{
			String s= service.path("webservices").path("marketi").path("market").path("novi").header("username", user).header("password", pass).type(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML).put(String.class, m);
			assertEquals(true, s.equals("true"));
			assertNotNull(s);
		}catch(Exception ex){
			System.out.println(ex.getMessage());
		}
	}*/
	
	@Test
	public void testObrisiMarketDELETE(){
		String id="10";
	//	service.path("webservices").path("marketi").path("market").path("obrisi").header("username", "KKO1").header("password","pass").header("id", id).type(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML).delete();
	}
	
	
	@Test
	public void testObrisiMarketPOST(){
		String id="10";
	//	String s= service.path("webservices").path("marketi").path("market").path("obrisi").header("username", "KKO1").header("password","pass").type(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML).post(String.class, id);
	//	assertEquals(true, s.equals("true"));
	}	
	
	@Test 
	public void testVratiMarket() throws Exception{
		try{
			String id = "1";
			Market market= service.path("webservices").path("marketi").path("market").path(id).accept(MediaType.APPLICATION_XML).get(Market.class);
			System.out.println(market.getNaziv());
			assertNotNull(market);
		}catch(Exception ex){
			assertEquals(2, 3);
			System.out.println(ex.getMessage());
		}
	}
}
